package com.shbaiche.bifenba.ui.data;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.adapter.my.CollectionListAdapter;
import com.shbaiche.bifenba.model.GankResults;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.data.CompeteVo;
import com.shbaiche.bifenba.model.data.TeamDynamicMessage;
import com.shbaiche.bifenba.model.data.TeamInfoMessage;
import com.shbaiche.bifenba.model.video.ArticleVo;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;

import butterknife.BindView;
import cn.droidlover.xdroid.base.SimpleRecAdapter;
import cn.droidlover.xdroid.base.XLazyFragment;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;
import okhttp3.Call;

/**
 * 球队动态
 */

@SuppressLint("ValidFragment")
public class DataTeamDetailDynamicFragment extends XLazyFragment {

    @BindView(R.id.contentLayout)
    XRecyclerContentLayout contentLayout;

    CollectionListAdapter adapter;

    boolean isVideo = false;

    protected static final int PAGE_SIZE = 10;
    protected static final int MAX_PAGE = 100;

    // 球队编号
    private String teamId;

    // 比赛类型: 1足球 0篮球
    private String gameType;

    @SuppressLint("ValidFragment")
    public DataTeamDetailDynamicFragment() {
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        if(savedInstanceState!=null && savedInstanceState.containsKey("teamId")){
            teamId = savedInstanceState.getString("teamId");
            gameType = savedInstanceState.getString("gameType");
        }else{
            teamId = getArguments().getString("teamId");
            gameType = getArguments().getString("gameType");

        }

        initAdapter();
        loadData(1);
    }

    private void initAdapter() {
        setLayoutManager(contentLayout.getRecyclerView());
        contentLayout.getRecyclerView()
                .setAdapter(getAdapter());
        contentLayout.getRecyclerView()
                .setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener() {
                    @Override
                    public void onRefresh() {
                        loadData(1);
                    }

                    @Override
                    public void onLoadMore(int page) {
                        loadData(page);
                    }
                });

        contentLayout.loadingView(View.inflate(getContext(), R.layout.view_loading, null));
        contentLayout.getRecyclerView().useDefLoadMoreView();
        contentLayout.getEmptyView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contentLayout.showLoading();
                loadData(1);
            }
        });
    }

    public void loadData(final int page) {
        TeamDynamicMessage message = new TeamDynamicMessage();
        message.setTeamId(teamId);
        message.setGameType(gameType);
        message.setPageIndex(page);
        message.setPageSize(PAGE_SIZE);

        NetApi.teamDynamic(message, new JsonCallback<TransMessage>() {
            @Override
            public void onFail(Call call, Exception e, int id) {
                ToastUtil.show("onFail" + e.getMessage());
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.show("onFail" + e.getMessage());

            }

            @Override
            public void onResponse(TransMessage response, int id) {

                TeamDynamicMessage result = new Gson().fromJson(response.getMessage(), TeamDynamicMessage.class);
                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, result.getCode())) {
                    if (result != null) {
                        Log.e("t", "onResponse");
                        if (page > 1) {
                            getAdapter().addData(result.getDynamic());
                        } else {
                            getAdapter().setData(result.getDynamic());
                        }

                        contentLayout.getRecyclerView().setPage(page, MAX_PAGE);

                        if (getAdapter().getItemCount() < 1) {
                            contentLayout.showEmpty();
                            return;
                        }

                    }
                } else {
                    Log.e("t", "onResponse getCode " + response.getCode());
                    ToastUtil.show(result.getMessage());
                }
            }
        });
    }


    public SimpleRecAdapter getAdapter() {
        if (adapter == null) {
            adapter = new CollectionListAdapter(getActivity(),isVideo);
            adapter.setRecItemClick(new RecyclerItemCallback<ArticleVo, CollectionListAdapter.ViewHolder>() {
                @Override
                public void onItemClick(int position, ArticleVo model, int tag, CollectionListAdapter.ViewHolder holder) {
                    super.onItemClick(position, model, tag, holder);
                }
            });
        }
        return adapter;
    }


    public void setLayoutManager(XRecyclerView recyclerView) {
        recyclerView.verticalLayoutManager(context);
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_collection;
    }

}
